package admin.service.impl;

import admin.entity.Checkitem;
import admin.mapper.CheckitemMapper;
import admin.service.CheckitemService;
import admin.util.PageQuery;
import admin.util.PageResult;
import admin.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Resource
    private CheckitemMapper checkitemMapper;

    @Override
    public PageResult findPageInfo(PageQuery pageQuery){
        //判断是否携带模糊查询的条件
        String queryString = pageQuery.getQueryString();
        //条件查询用LambdaQueryWrapper
        LambdaQueryWrapper<Checkitem> lambdaQueryWrapper = null;/*简化代码*/
        if(queryString!=null && queryString.length()>0){
            lambdaQueryWrapper = new LambdaQueryWrapper<>(); //条件构造器
            //like模糊查询 用code 或者 name 做模糊检索
            lambdaQueryWrapper.like(Checkitem::getCode,queryString);
            lambdaQueryWrapper.or();//两个条件是 或 的关系
            lambdaQueryWrapper.like(Checkitem::getName,queryString);
        }


        //当前页 每页显示的条数
        Page<Checkitem> page = new Page<>(pageQuery.getCurrentPage()
                                     , pageQuery.getPageSize());
        checkitemMapper.selectPage(page,lambdaQueryWrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public Result deleteInfoById(Integer id){
        //获取删除的结果
        boolean flag = checkitemMapper.deleteById(id)>0;
        if(flag){
            return Result.ok();
        }
        else {
            return Result.fail();
        }
    }

    @Override
    public Result addItemInfo(Checkitem checkitem){
        boolean flag = false;
        if(checkitem.getId()!=null){
            //更新数据操作
            flag = checkitemMapper.updateById(checkitem)>0;
        }
        else{
            flag = checkitemMapper.insert(checkitem)>0;
        }
        if(flag){
            return Result.ok();
        }
        else {
            return Result.fail();
        }
    }

    @Override
    public Result getItemInfoById(Integer id){
        Checkitem checkitem = checkitemMapper.selectById(id);
        return Result.ok().data(checkitem);
    }

    @Override
    public Result getAllItemInfo(){
        List<Checkitem> checkitems = checkitemMapper.selectList(null);
        return  Result.ok().data(checkitems);
    }
}
